package com.mysteel.cyx.demo.biz.threadlocal.keyAffinityExecutor;

import com.github.phantomthief.pool.KeyAffinityExecutor;

import java.util.ArrayList;
import java.util.List;

/**
 * KeyAffinityExecutor 是一个特殊的任务线程池。
 * 它可以确保投递进来的任务按 Key 相同的任务依照提交顺序依次执行。在既要通过并行处理来提高吞吐量、又要保证一定范围内的任务按照严格的先后顺序来运行的场景下非常适用。
 *
 * @Author：cyx
 * @Date：2024/4/15 11:27
 */
public class KeyAffinityExecutorTest {


    public static void main(String[] args) {
        KeyAffinityExecutor<Object> listeningExecutorServices = KeyAffinityExecutor.newSerializingExecutor(8, 200, "MY-POOL-%d");


        List<CoderDoSomeThing> coderDoSomeThingList = new ArrayList<>();

        coderDoSomeThingList.add(new CoderDoSomeThing("富贵", "启动Idea"));
        coderDoSomeThingList.add(new CoderDoSomeThing("富贵", "搞数据库,连tomcat,crud一顿输出"));
        coderDoSomeThingList.add(new CoderDoSomeThing("富贵", "嘴角疯狂上扬"));
        coderDoSomeThingList.add(new CoderDoSomeThing("富贵", "接口访问报错"));
        coderDoSomeThingList.add(new CoderDoSomeThing("富贵", "心态崩了，卸载Idea"));

        coderDoSomeThingList.add(new CoderDoSomeThing("旺财", "启动Idea"));
        coderDoSomeThingList.add(new CoderDoSomeThing("旺财", "搞数据库,连tomcat,crud一顿输出"));
        coderDoSomeThingList.add(new CoderDoSomeThing("旺财", "嘴角疯狂上扬"));
        coderDoSomeThingList.add(new CoderDoSomeThing("旺财", "接口访问报错"));
        coderDoSomeThingList.add(new CoderDoSomeThing("旺财", "心态崩了，卸载Idea"));

        coderDoSomeThingList.forEach(coderDoSomeThing -> {
            listeningExecutorServices.executeEx(coderDoSomeThing.getName(), () -> {
                System.out.println(Thread.currentThread().getName() + ":" + coderDoSomeThing.toString());
            });
        });
    }
}
